package com.ptp.backendcertms.business.controller;

import com.ptp.backendcertms.api.external.dto.req.CertificateDeleteReqDto;
import com.ptp.backendcertms.api.external.dto.req.CertificateDetailReqDto;
import com.ptp.backendcertms.api.external.dto.req.CertificateListReqDto;
import com.ptp.backendcertms.api.external.dto.req.CertificateUpdateReqDto;
import com.ptp.backendcertms.api.external.dto.res.CertificateDetailResDto;
import com.ptp.backendcertms.api.external.dto.res.CertificateListResDto;
import com.ptp.backendcertms.api.external.dto.res.ResponseDto;
import com.ptp.backendcertms.api.external.dto.res.ResponseEmptyDto;
import com.ptp.backendcertms.basiccompnents.http.ResponseWrapper;
import com.ptp.backendcertms.business.service.ICertificateBusinessService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 证书 业务控制器
 *
 * @author songcongcong
 * @date 2025/5/3
 */
@RestController
@RequestMapping("/cms")
public class CertificateBusinessController {
    @Resource
    private ICertificateBusinessService certificateBusinessService;

    /**
     * 证书列表查询
     *
     * @param reqDto 请求参
     */
    @RequestMapping(value = "/certificate/queryList", method = RequestMethod.POST)
    public ResponseDto<CertificateListResDto> getCertificateList(@RequestBody @Valid CertificateListReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseReturnWithValid(bindingResult, "[证书管理]-证书列表查询",
                () -> certificateBusinessService.getCertificateList(reqDto));
    }

    /**
     * 证书详情查询
     *
     * @param reqDto 请求参
     */
    @RequestMapping(value = "/certificate/queryDetail", method = RequestMethod.POST)
    public ResponseDto<CertificateDetailResDto> getCertificateDetail(@RequestBody @Valid CertificateDetailReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseReturnWithValid(bindingResult, "[证书管理]-证书详情查询",
                () -> certificateBusinessService.getCertificateDetail(reqDto.getSerialNo()));
    }

    /**
     * 证书删除接口
     */
    @RequestMapping(value = "/certificate/delete", method = RequestMethod.POST)
    public ResponseEmptyDto delete(@RequestBody @Valid CertificateDeleteReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseWithValid(bindingResult, "[证书管理]-删除证书", () ->
                certificateBusinessService.deleteCertificate(reqDto));
    }


    /**
     * 证书编辑接口
     */
    @RequestMapping(value = "/certificate/update", method = RequestMethod.POST)
    public ResponseEmptyDto update(@RequestBody @Valid CertificateUpdateReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseWithValid(bindingResult, "[证书管理]-编辑证书", () ->
                certificateBusinessService.updateCertificate(reqDto));
    }

}
